package cn.school.demo.ui.answer

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Answer
import cn.school.demo.pojo.Result
import cn.school.demo.pojo.Subject
import cn.school.demo.pojo.Test
import cn.school.demo.repository.AnswerRepository
import com.blankj.utilcode.util.LogUtils
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.ChronoUnit
import java.util.*

class AnswerViewModel : ViewModel() {
    private val _title = MutableLiveData<String>()
    val title: LiveData<String> get() = _title

    private val _introduction = MutableLiveData<String>()
    val introduction: LiveData<String> get() = _introduction

    private val _number = MutableLiveData<Int>()
    val number: LiveData<Int> get() = _number

    private val _test = MutableLiveData<Test>()
    val test: LiveData<Test> get() = _test

    private val _subject = MutableLiveData<Subject>()
    val subject: LiveData<Subject> get() = _subject

    private val _chooseButton = MutableLiveData<Int>()
    val chooseButton: LiveData<Int> get() = _chooseButton

    @SuppressLint("NewApi")
    private val _startTime = LocalDateTime.now()

    @SuppressLint("NewApi")
    private var _endTime = LocalDateTime.now()
    val endTime: LocalDateTime get() = _endTime

    private lateinit var _origin: String
    val origin: String get() = _origin

    private val allNum =
        if (MyApplication.subjectList.size > MyApplication.testList.size) MyApplication.subjectList.size else MyApplication.testList.size

    init {
        _number.value = 0
        _test.value = MyApplication.testList[0]
        _title.value = ""
        _introduction.value = ""
        _chooseButton.value = -1
        _subject.value =
            MyApplication.subjectList[0]
        MyApplication.answerScore.errorNum = allNum - MyApplication.answerScore.trueNumber
    }

    fun setChooseButton(b: Int) {
        if (b in 1..4)
            _chooseButton.value = b
    }

    fun setTest(t: Test) {
        _test.value = t
    }

    fun setSubject(s: Subject) {
        _subject.value = s
    }

    fun setTitle(title: String) {
        _title.value = title
    }

    fun setIntroduction(introduction: String) {
        _introduction.value = introduction
    }

    @SuppressLint("NewApi")
    fun setScore(score: Int) {
        MyApplication.answerScore.apply {
            ++trueNumber
            errorNum = allNum - trueNumber
            integralNum += score
            correctRate =
                getFloatNoMoreThanTwoDigits((trueNumber / allNum.toDouble()) * 100)
            useTime = getUseTime(_startTime, _endTime).toString()
        }
    }

    fun plusNumber() {
        if (_number.value!! < MyApplication.testList.size - 1 || _number.value!! < MyApplication.subjectList.size - 1)
            _number.value = _number.value?.plus(1)
    }

    fun minusNumber() {
        if (_number.value!! > 0)
            _number.value = _number.value?.minus(1)
    }

    @SuppressLint("NewApi")
    fun getUseTime(start: LocalDateTime, end: LocalDateTime): LocalTime? {
        val l = ChronoUnit.SECONDS.between(start, end)
        return LocalTime.ofSecondOfDay(l)
    }

    fun setEndTime(endTime: LocalDateTime) {
        _endTime = endTime
        MyApplication.answerScore.useTime = getUseTime(_startTime, _endTime).toString()
    }

    private fun getFloatNoMoreThanTwoDigits(number: Double): Float {
        val b: BigDecimal = BigDecimal(number)
        val f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).toFloat()
        return f1
    }

    fun setOrigin(origin: String) {
        _origin = origin
    }

    fun saveIntegral(): LiveData<Result> {
        LogUtils.e(MyApplication.answerScore.toString())
        val map = mapOf(
            "sfNumber" to MyApplication.user!!.sfNumber,
            "sign" to 3,
            "subjectName" to _origin,
            "correctQuantity" to MyApplication.answerScore.trueNumber
        )
        return AnswerRepository().saveAnswerIntegral(map)
    }

    fun getSubjectListById(): LiveData<MutableList<Subject>> =
        AnswerRepository().getSubjectListById(MyApplication.signId, MyApplication.user!!.sfNumber)

    fun saveAnswer(): LiveData<Result> {
        LogUtils.e(_startTime, _endTime, subject.value!!.signid,MyApplication.answerScore.integralNum)
        return AnswerRepository().saveAnswer(
            Answer(
                subject.value!!.subjectId,
                MyApplication.user!!.sfNumber,
                subject.value!!.signid,
                _startTime.toString(),
                _endTime.toString(),
                MyApplication.answerScore.integralNum
            )
        )
    }

}